package health

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/health"
	healthReq "gitee.com/g-qs/gin-vue-admin/server/model/health/request"
	"gorm.io/gorm"
)

type FdEfficacyService struct {
}

// CreateFdEfficacy 创建功效记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdEfficacyService *FdEfficacyService) CreateFdEfficacy(fdEfficacy *health.FdEfficacy) (err error) {
	err = global.GVA_DB.Create(fdEfficacy).Error
	return err
}

// DeleteFdEfficacy 删除功效记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdEfficacyService *FdEfficacyService) DeleteFdEfficacy(fdEfficacy health.FdEfficacy) (err error) {
	err = global.GVA_DB.Delete(&fdEfficacy).Error
	return err
}

// DeleteFdEfficacyByIds 批量删除功效记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdEfficacyService *FdEfficacyService) DeleteFdEfficacyByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]health.FdEfficacy{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateFdEfficacy 更新功效记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdEfficacyService *FdEfficacyService) UpdateFdEfficacy(fdEfficacy health.FdEfficacy) (err error) {
	err = global.GVA_DB.Save(&fdEfficacy).Error
	return err
}

// GetFdEfficacy 根据id获取功效记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdEfficacyService *FdEfficacyService) GetFdEfficacy(id uint) (fdEfficacy health.FdEfficacy, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&fdEfficacy).Error
	return
}

// GetFdEfficacyInfoList 分页获取功效记录
// Author [piexlmax](https://github.com/piexlmax)
func (fdEfficacyService *FdEfficacyService) GetFdEfficacyInfoList(info healthReq.FdEfficacySearch) (list []health.FdEfficacy, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&health.FdEfficacy{})
	var fdEfficacys []health.FdEfficacy
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&fdEfficacys).Error
	return fdEfficacys, total, err
}

func (fdEfficacyService *FdEfficacyService) SetFdEfficacyGraphic(req healthReq.FdEfficacyArticleReq) (err error) {
	var FdEfficacyGraphic []healthReq.FdEfficacyArticle
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		err = tx.Where("fd_efficacy_id = ?", req.FdEfficacyID).Delete(&FdEfficacyGraphic).Error
		if err != nil {
			return err
		}
		var efficacyArticles []healthReq.FdEfficacyArticle
		for _, graphic := range req.Graphics {
			efficacyArticles = append(efficacyArticles, healthReq.FdEfficacyArticle{
				FdEfficacyID: req.FdEfficacyID,
				ArticleID:    graphic,
				Type:         "graphic",
			})
		}
		for _, audio := range req.Audios {
			efficacyArticles = append(efficacyArticles, healthReq.FdEfficacyArticle{
				FdEfficacyID: req.FdEfficacyID,
				ArticleID:    audio,
				Type:         "audio",
			})
		}
		for _, video := range req.Videos {
			efficacyArticles = append(efficacyArticles, healthReq.FdEfficacyArticle{
				FdEfficacyID: req.FdEfficacyID,
				ArticleID:    video,
				Type:         "video",
			})
		}

		err = tx.Create(&efficacyArticles).Error
		return err
	})
}

func (fdEfficacyService *FdEfficacyService) GetFdEfficacyGraphic(fd_efficacy_id string) (DiseaseGraphic []healthReq.FdEfficacyArticle, err error) {
	err = global.GVA_DB.Where("fd_efficacy_id = ?", fd_efficacy_id).Find(&DiseaseGraphic).Error
	return
}
